from multipage.items import MultipageItem
import scrapy

'翻页爬虫'

class MultSpider(scrapy.Spider):
    name = 'mult'
    allowed_domains = ['yingjiesheng.com']
    # start_urls = ['https://www.yingjiesheng.com/commend-fulltime-1.html']

# 翻页函数
    def start_requests(self):
        for i in range(1,4):
            url = 'http://www.yingjiesheng.com/commend-fulltime-{}.html'.format(i)
            yield scrapy.Request(url=url)


    def parse(self, response):
        tr_list = response.xpath("//table[@class='jobul']/tr")
        items = []
        for tr in tr_list:
            # 一定要加入extract_first()，否则回传的是一串html代码
            title = tr.xpath("./td[1]/a/text()").extract_first()
            job_name = tr.xpath("./td[2]/a/text()").extract_first()
            # 创建一个实体类对象
            item = MultipageItem()
            item['title'] = title
            item['job_name'] = job_name
            items.append(item)
        # 返回到管道（pipe）里面
        return items
